package dubbo.server2.impl.elasticjob;

import com.alibaba.dubbo.common.json.JSONObject;
import com.alibaba.fastjson.JSON;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.dataflow.DataflowJob;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
@Component
public class MyDataflowJob implements DataflowJob<String> {
    private Logger logger = Logger.getLogger(MyDataflowJob.class);
    static List<String> list=new ArrayList<>();
    static {
        list.add("刘彻");
        list.add("王林");
        list.add("向平");
    }
    @Override
    public List<String> fetchData(ShardingContext shardingContext) {

        List<String> list2=new ArrayList<>();
        if(list.size()>0){
            String temp=list.get(0);
            list.remove(temp);
            list2.add(temp);
        }
        logger.info(String.format("----fetchData--Thread ID: %s, 任务总片数: %s, " +
                        "当前分片项: %s.当前参数: %s," +
                        "当前任务名称: %s.当前任务参数: %s,"+"返回list: %s"
                , Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter(),
                JSON.toJSONString(list2)
        ));
        return list2;
    }

    @Override
    public void processData(ShardingContext shardingContext, List<String> list) {
        logger.info(String.format("----processData--Thread ID: %s, 任务总片数: %s, " +
                        "当前分片项: %s.当前参数: %s," +
                        "当前任务名称: %s.当前任务参数: %s."+"获取list: %s"
                ,
                Thread.currentThread().getId(),
                shardingContext.getShardingTotalCount(),
                shardingContext.getShardingItem(),
                shardingContext.getShardingParameter(),
                shardingContext.getJobName(),
                shardingContext.getJobParameter(),
                JSON.toJSONString(list)
        ));
    }
}
